package com.aispace.supersql.example.controller;

import com.aispace.supersql.model.ChatClientFactory;
import lombok.AllArgsConstructor;
import org.springframework.ai.azure.openai.AzureOpenAiChatModel;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.document.Document;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


@RestController
@RequestMapping("super")
@AllArgsConstructor
public class SuperChatController {

    private final AzureOpenAiChatModel chatModel;

    @GetMapping("/chat")
    public String chat(@RequestParam String question) {
        PromptTemplate promptTemplate = new PromptTemplate("""
                你是一个AI私人助理，你叫小瓜。用户问的问题是:
                '{question}'
                请用私人助理的口吻回答用户问题
                """);
        Prompt prompt = promptTemplate.create(Map.of("question", question));
        String response = ChatClientFactory.buildChatClient(this.chatModel)
                .prompt(prompt)
                .call()
                .content();
        return response;
    }


    public static void main(String[] args) {
        String intermediateSql="select * from dtp_hospital where district like '%黄浦区%' ";
        String executed = "{'district': '黄浦区', 'hospital_name': '黄浦区人民医院', 'hospital_id': '1001'}";

        Document document = new Document(String.format("""
                        The following is a pandas DataFrame with the results of the intermediate SQL query %s:\\n%s
                        """,
                intermediateSql, executed.toString()
        ));
        System.out.println(document.getText());
    }
}
